const app = getApp();
Page({
  data: {
    //选择年月日
    dateText: '',
    dateVisible: false,
    date: '2025-07-24',
    mode: '',
    //上传身份证
    fileList: [],
    //访客信息表单
    orderForm: {
      createTime: "",
      identity: "",
      phone: "",
      unit: "",
      information: "",
      intervieweeName: "",
      area: "",
      reception: "",
      accompany: "",
      identityOss: "",
      visitorDataEntourageList: [
        { name: "", phone: "", }
      ],
      visitorDataVehicleList: [
        { number: "" }
      ],
    },
  },
  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
      [`orderForm.createTime`]: value
    });
    this.hidePicker();
  },
  handleInputChange(e) {
    let value = e.detail.value;
    let fieldName = e.target.dataset.fieldName;
    this.setData({
      [`orderForm.${fieldName}`]: value
    })
  },
  handleAdd(e) {
    let that = this;
    const { fileList } = this.data;
    const { files } = e.detail;
    this.setData({
      fileList: [...fileList, ...files], // 此时设置了 fileList 之后才会展示选择的图片
    });
    wx.uploadFile({
      url: 'https://xuexiwuapi.jomoit.com/visitor-api/common/upload', // 仅为示例，非真实的接口地址
      filePath: e.detail.files[0].url,
      name: 'file',
      formData: { user: 'test' },
      header: {
        Authorization: 'Bearer ' + app.globalData.token
      },
      success: (res) => {
        const { data } = res;
        const { code, msg } = JSON.parse(data);
        that.setData({
          [`orderForm.identityOss`]: msg
        })
      },
    });
  },
  handleRemove(e) {
    const { index } = e.detail;
    const { fileList } = this.data;
    fileList.splice(index, 1);
    this.setData({
      fileList,
    });
  },
  onSubmit() {
    let that = this;
    wx.request({
      url: 'https://xuexiwuapi.jomoit.com/visitor-api/dataApi',
      method: 'POST', 
      header: {
        "Content-Type": "application/json",
        Authorization: 'Bearer ' + app.globalData.token
      },
      data: {
        ...that.data.orderForm,
        createTime: "2023-04-12 00:12:45",
        identity: "141181199407250123",
        intervieweeName: "admin",
        information: "见朋友",
        area: "工厂",
        reception: "开发部",
        phone: "18734804473",
        visitorDataEntourageList: [
          { name: "admin1", phone: "12", }
        ],
        visitorDataVehicleList: [
          { number: "1" }
        ],
      },
      success: function(res) {
        const { data } = res;
        const { code, msg } = data;
        
      }
    })
  },



  onLoad(options) {},
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
})